package org.aeon.fasttasks.core.util;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;

import java.util.Objects;

/**
 * @author aeon
 * @createTime 2025/08/11  17:21
 * @description
 */
public class PackageUtils {

    /**
     * 获取启动类所在包名
     *
     * @param ctx
     * @return
     */
    public static String getBasePackage(ApplicationContext ctx) {
        Environment env = ctx.getEnvironment();
        String mainClassName = Objects.requireNonNull(env.getProperty("sun.java.command")).split(" ")[0];
        try {
            Class<?> mainClass = Class.forName(mainClassName);
            SpringBootApplication app = mainClass.getAnnotation(SpringBootApplication.class);
            if (app != null) {
                return mainClass.getPackage().getName();
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
